package com.pxmeta.dynamic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.circle.entity.Circle;
import com.pxmeta.circle.service.ICircleService;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.dynamic.entity.TopicCategory;
import com.pxmeta.dynamic.entity.vo.TopicCategoryVO;
import com.pxmeta.dynamic.mapper.TopicCategoryMapper;
import com.pxmeta.dynamic.service.ITopicCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-07-12
 */
@Service
@RequiredArgsConstructor
public class TopicCategoryServiceImpl extends ServiceImpl<TopicCategoryMapper, TopicCategory> implements ITopicCategoryService {
    private final ICircleService circleService;

    /**
     * 获取话题分类列表
     * @param circleId
     * @return
     */
    @Override
    public List<TopicCategory> getCategoryByCircleType(Long circleId) {
        Circle circle = circleService.getById(circleId);
        if (null == circle){
            throw new ServiceException("圈子不存在！");
        }
        Integer type = circle.getType();
        LambdaQueryWrapper<TopicCategory> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TopicCategory::getCircleType, type)
                .orderByDesc(TopicCategory::getSort);
        return this.list(wrapper);
    }

    @Override
    public IPage<TopicCategoryVO> getList(Integer page, Integer size, String keyword, Integer type) {
        return baseMapper.getList(new Page<>(page, size), keyword, type);
    }
}
